﻿// -------------------------------------------------------------------------------------------------------------------------------
// <copyright file="TestAssignIndexedDenseVector.cs" company="Exul">
//     Copyright (c) Exul. All rights reserved.
// </copyright>
// <author>
//     Exul
// </author>
// -------------------------------------------------------------------------------------------------------------------------------

namespace ExulLibrary.Mathematics.LinearAlgebra.Sequential.Tests.DenseVectorTest
{
    #region Usings

    using System;
    using System.Globalization;

    using ExulLibrary.Exceptions;

    using NUnit.Framework;

    #endregion

    /// <summary>
    ///     Test suite for <see cref="DenseVectorExtensions.Assign{T}(DenseVector{T},int[],DenseVector{T})"/>.
    /// </summary>
    [TestFixture]
    [Category("ExulLibrary.Mathematics.LinearAlgebra.Sequential.DenseVectorExtensions")]
    public sealed class TestAssignIndexedDenseVector
    {
        /// <summary>
        ///     Tests the method when indexes specify the existing elements.
        /// </summary>
        [Test]
        public void WhenTargetIndexesAreValid()
        {
            // Arrange
            var target = new DenseVector<double>(new double[] { 10, 20, 30 });
            var source = new DenseVector<double>(new double[] { 1, 2 });
            var targetIndexes = new[] { 0, 2 };

            // Act
            target.Assign(targetIndexes, source);

            // Assert
            Assert.That(target.Values, Is.EquivalentTo(new double[] { 1, 20, 2 }));
        }

        /// <summary>
        ///     Tests the method when targetIndexes parameter is empty collection.
        /// </summary>
        [Test]
        public void WhenTargetIndexesCollectionIsEmpty()
        {
            // Arrange
            var target = new DenseVector<double>(new double[] { 10, 20, 30 });
            var source = new DenseVector<double>(new double[] { 1, 2 });
            var targetIndexes = new int[0];

            // Act
            target.Assign(targetIndexes, source);

            // Assert
            Assert.That(target.Values, Is.EquivalentTo(new double[] { 10, 20, 30 }));
        }

#if SAFE

        /// <summary>
        ///     Tests the method when target parameter is <see langword="null"/>.
        /// </summary>
        [Test]
        public void WhenTargetIsNull()
        {
            // Arrange
            var source = new DenseVector<double>(new double[] { 1, 2 });
            var targetIndexes = new[] { 0, 2 };

            // Act and Assert
            Assert.That(
                () => DenseVectorExtensions.Assign(null, targetIndexes, source),
                Throws.TypeOf<ArgumentNullException>().With.Property("ParamName").EqualTo("target"));
        }

        /// <summary>
        ///     Tests the method when targetIndexes parameter is <see langword="null"/>.
        /// </summary>
        [Test]
        public void WhenTargetIndexesIsNull()
        {
            // Arrange
            var target = new DenseVector<double>(new double[] { 10, 20, 30 });
            var source = new DenseVector<double>(new double[] { 1, 2 });

            // Act and Assert
            Assert.That(
                () => target.Assign(null, source),
                Throws.TypeOf<ArgumentNullException>().With.Property("ParamName").EqualTo("targetIndexes"));
        }

        /// <summary>
        ///     Tests the method when source parameter is <see langword="null"/>.
        /// </summary>
        [Test]
        public void WhenSourceIsNull()
        {
            // Arrange
            var target = new DenseVector<double>(new double[] { 10, 20, 30 });
            var targetIndexes = new[] { 0, 2 };

            // Act and Assert
            Assert.That(
                () => target.Assign(targetIndexes, null),
                Throws.TypeOf<ArgumentNullException>().With.Property("ParamName").EqualTo("source"));
        }

        /// <summary>
        ///     Tests the method when an index of element is negative or greater than or equal to
        ///     the number elements of the target vector.
        /// </summary>
        /// <param name="position">
        ///     The position of index in the storage.
        /// </param>
        /// <param name="targetIndex">
        ///     The index of target element.
        /// </param>
        [Test]
        public void WhenATargetIndexIsOutOfRange([Values(0, 1)] int position, [Values(-1, 3)] int targetIndex)
        {
            // Arrange
            var target = new DenseVector<double>(3);
            var source = new DenseVector<double>(2);
            var targetIndexes = new int[2];
            targetIndexes[position] = targetIndex;

            // Act and Assert
            var parameterName = string.Format(CultureInfo.InvariantCulture, "targetIndexes[{0}]", position);
            Assert.That(
                () => target.Assign(targetIndexes, source),
                Throws.TypeOf<ElementIndexIsOutOfRangeException>().With.Property("ParamName").EqualTo(parameterName));
        }

#endif
    }
}